// local.strategy.ts
import { Injectable } from '@nestjs/common';
import { PassportStrategy } from '@nestjs/passport';
import { Strategy } from 'passport-local';
import { AuthService } from './auth.service';
import { CustomException, ErrorCode } from '@/common/exceptions/custom.exception';

@Injectable()
export class LocalStrategy extends PassportStrategy(Strategy) {
  constructor(private readonly authService: AuthService) {
    super({
      usernameField: 'email',
    });
  }

  // 登录验证
  async validate(email: string, password: string): Promise<any> {
    const user = await this.authService.validateEmail(email, password);
    if (!user) throw new CustomException(ErrorCode.ERR_10005);
    return user;
  }
}
